<?php
	class Config {
		
		
		var $values = array();
		function getTemplateDir(){
			return $this->values["configuration"]["template_dir"];
		}
		function getUser($login){
			return isset($login) && isset($this->values['users'][$login]) ? 
					array(	'login' 	=> $login, 
							'password' 	=> $this->values['users'][$login][0], 
							'role' 		=> $this->values['users'][$login][1] ) :
					null;
		}
		
		function invokeInitFunction($params = null){
			return $this->_invoke($this->values["configuration"]["init_function"], $params);
		}
		function invokeEndFunction($params = null){
			return $this->_invoke($this->values["configuration"]["end_function"], $params);
		}
		
		function invokeMapping($q, $sess, $params = null){
			$mapping = $this->getMapping($q);
			$mapping = ($mapping == null) ? $this->getDefaultMapping() : ( ($this->hasAccess($mapping[2], $sess)) ? $mapping : $this->getDefaultMapping() );
			return $this->_invoke($mapping, $params);
		}
		
		var $allReadyInclude = array();
		function _invoke($mapping, $params = null){
			if (is_array($mapping) && count($mapping) >= 2){
				$require = './controller/'.$mapping[0];
				if (!in_array($require, $this->allReadyInclude)){
					array_push($this->allReadyInclude, $require);
					require $require;
				}
				//echo "invoke ".$mapping[0]." => |" . $mapping[1]. "|<br>";
				return call_user_func($mapping[1], $params);
			}
			return false;
		}
		
		function hasAccess($requiredRole, $ses){
			$rolesValue = array(
				'ALL' 	=> 0,
				'PUBLIC'=> 10,
				'USER' 	=> 20,
				'GEST' 	=> 30,
				'ADMIN'	=> 99
			);
			//echo 'requiredRole=' . $requiredRole . '|currentRole=' . $this->getCurrentRole($ses);
			return isset($rolesValue[$requiredRole]) &&  $rolesValue[$requiredRole] <= $rolesValue[$this->getCurrentRole($ses)];
		}
		
		function getCurrentRole($ses){
			
			if (isset($ses[$this->values["configuration"]["attribute_name_session_role"]]))
				return $ses[$this->values["configuration"]["attribute_name_session_role"]];
			else
				return "PUBLIC";
		}
		
		function getMapping($q){
			return isset($this->values["mapping"]["/".$q]) ? $this->values["mapping"]["/".$q] : null;
		}
		
		function getDefaultMapping(){
			return $this->values["mapping"]["default"];
		}
		
		function parse_ini_file($filepath) {
			//echo $filepath;
			if (is_file($filepath) && ($lines = file($filepath)) != false) {
				for($i = 0; $i < count($lines); $i++){
					$line = $lines[$i];
					if (substr($line, 0, 1) != "#" && (substr($line, 0, 1) == "[" || strpos($line, "=") != false)){
						
						if (substr($line, 0, 1) == "["){
							if (!empty($sectionname)){
								$this->values[$sectionname] = $sectionvalue;
							}
							$sectionname = substr($line, 1, strlen($line)-4);
							if (isset($this->values[$sectionname])){
								$sectionvalue = $this->values[$sectionname];
							}
							else{
								$sectionvalue = array();
							}
						}
						else{
							$key = trim(substr($line, 0, strpos($line, "=")));
							$value = trim(substr($line, strpos($line, "=")+1), " \t");
							$value = str_replace("\t", " ", $value);
							$count = 1;
							while($count != 0){
								$value = str_replace("  ", " ", $value, $count);
							}
							$value = trim($value);
							if (strpos($value, " ")){
								$value = explode(" ", trim($value));
							}
							$sectionvalue[$key] = $value;
						}
					}
				}
				$this->values[$sectionname] = $sectionvalue;
				
				//print_r($this->values);
			
				return true;
			}
			else{
				return false;
			}
		}
	}


?>